<template>
  <!-- 修改企业 -->
  <view class="amendBusiness">
    <view class="content">
      <view class="content-item">
        <u-form :model="form" ref="form" label-width="280" labelPosition="’top‘" :error-type="errorType">
          <u-form-item label="统一信用代码" borderBottom>
            <u--input v-model.trim="form.creditCode" border="none" placeholder="请填写" :maxlength="22"></u--input>
          </u-form-item>

          <u-form-item label="企业办公地址" borderBottom>
            <u--input v-model="form.addressDetail" disabledColor="#ffffff" placeholder="请填写新的办公地址" border="none"
              :maxlength="30"></u--input>
          </u-form-item>

          <u-form-item label="经纬度" borderBottom>
            <view class="map-form">
              <u--input v-model="lnglat" border="none" placeholder="在地图上选择点位" :maxlength="30"
                :readonly="true"></u--input>
              <view class="map-img" @click="mapHandle">
                <image :src="baseImgUrl + 'map-icon.png'"></image>
                点击选择
              </view>
            </view>
          </u-form-item>

          <u-form-item label="营业执照照片" borderBottom>
            <u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic(1)" name="file" :maxCount="1"
              width="110" height="90">
              <image :src="baseImgUrl + 'upload-icon.png'" style="width: 220rpx; height: 180rpx"></image>
            </u-upload>
          </u-form-item>

          <u-form-item label="负责人姓名" borderBottom>
            <u--input v-model="form.legalPersonName" disabledColor="#ffffff" placeholder="请填写" border="none"
              :maxlength="30"></u--input>
          </u-form-item>
          <u-form-item label="法人照片" borderBottom>
            <u-upload :fileList="fileList2" @afterRead="afterReadTwo" @delete="deletePic(2)" name="file" :maxCount="1"
              width="110" height="90">
              <image :src="baseImgUrl + 'upload-icon.png'" style="width: 220rpx; height: 180rpx"></image>
            </u-upload>
          </u-form-item>

          <u-form-item label="法人手机号码" borderBottom>
            <view class="codeForm">
              <u--input v-model="form.mobile" type="number" :maxlength="11" border="none" placeholder="请填写新的手机号码"
                :readonly="true"></u--input>
              <button class="lookbtn code-btn" @tap="mobileHandle">修改</button>
            </view>
          </u-form-item>
        </u-form>
      </view>
    </view>

    <view class="btns">
      <button class="reset-btn lt cancelBtn" type="primary" @click="resethandle">
        取消
      </button>
      <button class="submit-btn" type="primary" @click="submitHandle">
        确认
      </button>
    </view>
  </view>
</template>

<script>
  import {
    updateCompany
  } from "@/api/myself.js";
  import {
    findEnableDictItemsbyCode
  } from "@/api/api.js";
  import {
    Toast,
    uniTrim,
    checkSocialCardNumber,
    medicalTreatment,
    registerNumber,
    getHeader,
    uploadFile,
  } from "@/utils/util.js";

  export default {
    name: "amendBusiness",
    data() {
      return {
        baseImgUrl: this.$baseImgUrl,
        baseURL: this.$baseURL,
        options: {},
        form: {},
        errorType: ["none"],
        lnglat: "", //经纬度
        fileList: [],
        fileList2: [],
        imgPrefix: uni.getStorageSync("imgPrefix"),
      };
    },
    onLoad(options) {
      if (options.data) {
        uni.removeStorageSync("mobileNew");

        this.options = JSON.parse(decodeURIComponent(options.data));
        this.form.creditCode = this.options.companyInfo.creditCode;
        this.form.mobile = this.options.legalInfo.mobile;
        this.form.legalPersonName = this.options.legalInfo.legalPersonName;
        this.form.addressDetail = this.options.companyInfo.addressDetail;
        this.form.lng = this.options.lng;
        this.form.lat = this.options.lat;
        this.lnglat = `${this.options.lng}-${this.options.lat}`;
        this.form.businessLicenseUrl =
          this.options.companyInfo.businessLicenseUrl;
        this.fileList.push({
          url: this.imgPrefix + this.options.companyInfo.businessLicenseUrl,
        });

        this.form.personImage = this.options.legalInfo.personImage;
        this.fileList2.push({
          url: this.imgPrefix + this.options.legalInfo.personImage,
        });
      }
    },
    onShow() {
      // 新的法人手机号
      uni.$on("mobileNew", (v) => {
        this.form = Object.assign({}, {
          ...this.form,
          mobile: v,
        });
      });
    },
    methods: {
      mapHandle() {
        let _this = this;
        uni.chooseLocation({
          success: function(res) {
            let {
              longitude,
              latitude
            } = res;
            _this.form.lng = longitude;
            _this.form.lat = latitude;
            _this.lnglat = `${longitude}-${latitude}`;
          },
          fail: function(err) {
            console.log(err, "错误的");
          },
        });
      },
      mobileHandle() {
        let data = {
          id: this.options.companyInfo.id,
          mobile: this.options.legalInfo.mobile,
        };
        uni.navigateTo({
          url: `/selfInfo/amendBusiness/legalPersonphone?data=${encodeURIComponent(
          JSON.stringify(data)
        )}`,
        });
      },
      afterRead(info) {
        let _this = this;
        uploadFile({
            url: "/web/sys/upload/upload_file",
            filePath: info.file.url,
            name: "file",
            formData: {
              type: 2,
            },
          })
          .then((res) => {
            let {
              fullPath,
              halfPath
            } = res.result;
            _this.fileList.push({
              url: fullPath
            });
            _this.form.businessLicenseUrl = halfPath;
          })
          .finally(() => {})
      },
      afterReadTwo(info) {
        let _this = this;
        uploadFile({
            url: "/web/sys/upload/upload_file",
            filePath: info.file.url,
            name: "file",
            formData: {
              type: 2,
            }
          })
          .then((res) => {
            let {
              fullPath,
              halfPath
            } = res.result;
            _this.fileList2.push({
              url: fullPath
            });
            _this.form.personImage = halfPath;
          })
          .finally(() => {})
      },
      deletePic(num) {
        if (num == 1) {
          this.fileList = [];
          this.form.businessLicenseUrl = "";
        } else {
          this.fileList2 = [];
          this.form.personImage = "";
        }
      },
      resethandle() {
        uni.navigateBack();
      },
      submitHandle() {
        let {
          creditCode,
          addressDetail,
          businessLicenseUrl,
          legalPersonName,
          personImage,
          mobile,
        } = this.form;

        // if (!checkSocialCardNumber(creditCode)) {
        //   Toast("请输入正确的统一信用代码");
        //   return;
        // }

        let check = checkSocialCardNumber(creditCode);
        let medical = medicalTreatment(creditCode);
        let refisNum = registerNumber(creditCode);
        if (!check && !medical && !refisNum) {
          Toast("请输入正确的信用代码");
          return;
        }

        if (!addressDetail || !uniTrim(addressDetail)) {
          Toast("请输入企业办公地址");
          return;
        }

        if (!this.lnglat) {
          Toast("请选择经纬度");
          return;
        }

        if (!businessLicenseUrl) {
          Toast("请上传营业执照");
          return;
        }

        if (!legalPersonName || !uniTrim(legalPersonName)) {
          Toast("请输入法人姓名");
          return;
        }

        if (!personImage) {
          Toast("请上传法人照片");
          return;
        }

        let data = {
          // oldLegalKey: this.options.oldLegalKey,     后面记得放开
          companyMessageInfo: {
            id: this.options.companyInfo.id,
            creditCode: this.form.creditCode,
            addressDetail: addressDetail,
            lng: this.form.lng,
            lat: this.form.lat,
            businessLicenseUrl: businessLicenseUrl,
          },
          companyLegalPersonInfo: {
            personImage: personImage,
            legalPersonName: legalPersonName,
            mobile: mobile,
          },
        };

        updateCompany(data).then((res) => {
          if (res.code == 200) {
            Toast(res.message);
            setTimeout(() => {
              uni.navigateBack();
            }, 1000);
          } else if (res.code == 400) {
            Toast(res.message);
          }
        });
      },
    },
  };
</script>

<style lang="scss" scoped>
  .amendBusiness {
    flex: 1;
    background-color: #f9f9f9;

    .content {
      margin: 0 auto;
      margin-top: 20rpx;
      margin-bottom: 20rpx;
      border-radius: 8rpx;
      width: calc(100% - 35rpx);
      height: calc(100vh - 84px);
      overflow-y: auto;

      .content-item {
        padding: 25rpx;
        background: #fff;

        .map-form {
          display: flex;

          .map-img {
            color: #2488f5;
            font-size: 30rpx;
            font-weight: 500;

            image {
              display: inline-block;
              margin-right: 10rpx;
              vertical-align: top;
              width: 30rpx;
              height: 40rpx;
            }
          }
        }

        .code-btn {
          color: #2488f5;
          font-size: 28rpx;
        }

        .codeForm {
          display: flex;
          justify-content: space-around;
        }
      }
    }

    /deep/.u-form-item__body__left__content__label {
      color: #666;
    }
  }
</style>